package hoang.android.app.youtube;

import hoang.android.app.R;
import hoang.android.app.adapter.VideoAdapter;
import hoang.android.app.json.ParserJson;
import hoang.android.app.model.Video;
import hoang.android.app.network.AsyncHttpGet;
import hoang.android.app.network.AsyncHttpResponseProcess;
import hoang.android.app.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class ListVideo extends BaseActivity{
	
	public static final String KEY = "VIDEO_ID";
	
	private ArrayList<Video> arrayVideos;
	private ListView listVideo;
	private VideoAdapter adapterVideo;
	
//	public static String playlistID = "PLmahwPz1Oo3m5g8YCpsk72kfBi147I1cj";
	public static String playlistID = "UUn-WnIG_oFOd11uKHESF_9w";

	public static final String API = "https://gdata.youtube.com/feeds/api/playlists/"+playlistID+"?v=2&alt=json";
	
	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		setContentView(R.layout.listvideo);
		
		listVideo = (ListView)findViewById(R.id.listVideo);
		arrayVideos = new ArrayList<Video>();
		adapterVideo = new VideoAdapter(ListVideo.this, R.layout.item_video, arrayVideos);
		listVideo.setAdapter(adapterVideo);
		
		listVideo.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(ListVideo.this, DetailVideo.class);
				intent.putExtra(KEY, arrayVideos.get(arg2).getIdVideo());
				startActivity(intent);
			}
		});
		
		loadListVideo();
	}
	
	private void loadListVideo(){
		//buoc1: tao paramter
		List<NameValuePair> parameter = null;
		
		//buoc2: xay dung post, get
		AsyncHttpGet httpGet = new AsyncHttpGet(
				ListVideo.this, 
				new AsyncHttpResponseProcess(ListVideo.this, true){
					
					@Override
					public void processIfResponseSuccess(JSONObject json) {
						super.processIfResponseSuccess(json);
						//buoc4: xu li du lieu tra ve
						processGetListVideoSuccess(json);
					}
				},
				parameter);
		
		//buoc3: thuc hien gui request
		httpGet.execute(API);
	}
	
	private void processGetListVideoSuccess(JSONObject jsonObject){
		ArrayList<Video> array = new ArrayList<Video>();
		
		try {
			array = ParserJson.parserGetListVideo(jsonObject);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		for (int i = 0; i < array.size(); i++) {
			arrayVideos.add(array.get(i));
		}
		
		//update lai listview
		adapterVideo.notifyDataSetChanged();
	}
	

}
